// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import javax.swing.DefaultComboBoxModel;

import tc.crase.meta.Type;
import tc.crase.sys.XdbRoot;

public class XobjectTypeModel extends DefaultComboBoxModel {
	public Type typeSelected;
	
	void setSelected(String type) {
		for (int i = 0; i < getSize(); i++ )
		{	TypeModelItem item = (TypeModelItem)getElementAt(i);
			if (item == null) continue;
			if (type.equals(item.type.id)) { 
				setSelectedItem(item);
				return;
			}
		}
	}
	
	public XobjectTypeModel () {
		Type[] types = XdbRoot.xdbsys.current.metadata.types.values().toArray(new Type[0]);
		typeSelected = types[0];
		
		for (int i = 0; i < types.length; i++) {
			addElement(new TypeModelItem(types[i]));
		}
	}
	
	public void setSelectedItem(Object item)
	{	TypeModelItem itemEx = (TypeModelItem)item;
		typeSelected = itemEx.type;
		super.setSelectedItem(item);
	}
	
	public class TypeModelItem {
		Type type;
		
		TypeModelItem(Type type) {
			this.type = type;
		}
		
		public String toString() {
			return type.id;
		}
	}
}
